/*
 * Copyright 2010 Inera
 *
 *   This library is free software; you can redistribute it and/or modify
 *   it under the terms of version 2.1 of the GNU Lesser General Public
 *
 *   License as published by the Free Software Foundation.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the
 *   Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 *
 *   Boston, MA 02111-1307  USA
 */
package se.inera.ifv.casebox.services;

import javax.annotation.Resource;
import javax.jws.WebService;

import org.w3.wsaddressing10.AttributedURIType;

import se.inera.ifv.casebox.core.entity.Answer;
import se.inera.ifv.casebox.core.service.AnswerService;
import se.inera.ifv.receivemedicalcertificateanswer.v1.rivtabp20.ReceiveMedicalCertificateAnswerResponderInterface;
import se.inera.ifv.receivemedicalcertificateanswerresponder.v1.ReceiveMedicalCertificateAnswerResponseType;
import se.inera.ifv.receivemedicalcertificateanswerresponder.v1.ReceiveMedicalCertificateAnswerType;
import se.inera.ifv.v1.ResultCodeEnum;
import se.inera.ifv.v1.ResultOfCall;

@WebService(serviceName = "ReceiveMedicalCertificateAnswerResponderService", 
	    endpointInterface="se.inera.ifv.receivemedicalcertificateanswer.v1.rivtabp20.ReceiveMedicalCertificateAnswerResponderInterface", 
	    portName = "ReceiveMedicalCertificateAnswerResponderPort", 
	    targetNamespace = "urn:riv:insuranceprocess:healthreporting:ReceiveMedicalCertificateAnswer:1:rivtabp20",
	    wsdlLocation = "schemas/interactions/ReceiveMedicalCertificateAnswerInteraction/ReceiveMedicalCertificateAnswerInteraction_1.0_rivtabp20.wsdl")
public class ReceiveMedicalCertificateAnswerImpl implements ReceiveMedicalCertificateAnswerResponderInterface {
	
	private AnswerService answerService;
	
	@Resource
	public void setAnswerService(AnswerService answerService) {
		this.answerService = answerService;
	}
	
	public ReceiveMedicalCertificateAnswerResponseType receiveMedicalCertificateAnswer(
			AttributedURIType adress, ReceiveMedicalCertificateAnswerType parameters) {
		try {
			
			String careUnit = adress.getValue();
			
			Answer answer = new Answer(careUnit, parameters.getAnswer());
			answerService.saveAnswer(answer);
			
			ReceiveMedicalCertificateAnswerResponseType response = new ReceiveMedicalCertificateAnswerResponseType();
			
			ResultOfCall value = new ResultOfCall();
			value.setResultCode(ResultCodeEnum.OK);
			response.setResult(value );
			
			return response;
		} catch (Exception e) {
			System.out.println("Error occured: " + e);
			return null;
		}
	}
}
